<?php
require_once 'config.php'; 
require_once 'Tools.class.php';
require_once 'User.class.php'; 
require_once 'HTML.class.php'; 

class Courses{
    private $line1="-----------------\n";
    private $line2="===============\n";
    private $A=array("","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","");
    private $B=array("","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","");
    private $weekName = Array("日","一","二","三","四","五","六");
    private $className = Array("第1,2节","第3,4节","第5,6节","第7,8节","第9,10节");
    private $SummerTime=array("08:00-09:50","10:10-12:00","14:30-16:10","16:20-18:00","19:30-21:30");
    private $WinterTime=array("08:00-09:50","10:10-12:00","14:00-15:40","15:50-17:30","19:00-21:00");
    private $SummerTime2=array("08:00","09:50","10:10","12:00","14:30","16:10","16:20","18:00","19:30","21:30","08:00+1");
    private $WinterTime2=array("08:00","09:50","10:10","12:00","14:00","15:40","15:50","17:30","19:00","21:00","08:00+1");


    //设置课表对象，构造
    public function setCourses($xh){
	   $xh=User::getXH($xh);
       $strKBinfo = Tools::getHttp(DZU_URL."curriculum.asp?&xn=".XN."&xq=".XQ."&xh=".$xh); 
       $KBs=explode("@KCB", $strKBinfo);
       for($i=0;$i<count($KBs);$i++){
            $this->Save_KB($KBs[$i]);
       }
    }
    private function Save_KB($kb){
        $index=-1;//第几节课索引
        $index2="";//原始接口上课时间
        $isAB="AB";//单双周

        $sksj=explode(";", Tools::getInfo($kb,"SKSJ"));
        $skdd=explode(";", Tools::getInfo($kb,"SKDD"));
        for($i=0;$i<count($sksj);$i++){
            if(strpos($sksj[$i], "单周")>0){
               $isAB="A";
            }elseif(strpos($sksj[$i], "双周")>0){
                $isAB="B";
            }else{
               $isAB="AB";//单双周 
            }

            if(strpos($sksj[$i], "{")){//去掉花括号里边
                $index2=substr($sksj[$i],0,strpos($sksj[$i], "{"));
            }

            $tempIndex=-1;
            for($x=0;$x<7;$x++){
                for($x1=0;$x1<5;$x1++){
                    $tempIndex+=1;
                    if("周".$this->weekName[$x].$this->className[$x1]==$index2){
                        $index=$tempIndex;
                        break 2;
                    }
                }
            }

            $sksj2="";//上课具体时间
            if("夏季"==ZXB){
                $sksj2=$this->SummerTime[$index%5];
            }else{
                $sksj2=$this->WinterTime[$index%5];  
            }
            if(($isAB=="AB" || $isAB=="A") && $index>0){
                $this->A[$index]=Tools::getInfo($kb,'KCMC').'|'.Tools::getInfo($kb,'JSXM').'|'.Tools::getInfo($kb,'XKKH').'|'.Tools::getInfo($kb,'BJMC').'|'.Tools::getInfo($kb,'XF').'|'.$index2.'|'.$sksj2.'|'.$skdd[$i].'|'.$index;
            }
            if(($isAB=="AB" || $isAB=="B") && $index>0){
                $this->B[$index]=Tools::getInfo($kb,'KCMC').'|'.Tools::getInfo($kb,'JSXM').'|'.Tools::getInfo($kb,'XKKH').'|'.Tools::getInfo($kb,'BJMC').'|'.Tools::getInfo($kb,'XF').'|'.$index2.'|'.$sksj2.'|'.$skdd[$i].'|'.$index;
            }
        }//for
    }//function


    //####################################################################################

    //当前课程或下节课
    public function getNextClass(){
        $kIndex = array(0,0,1,1,2,2,3,3,4,4);

        if($this->getWeekOf20()%2==1){
            $k=$this->A;
        }else{
            $k=$this->B;
        }
        if("夏季"==ZXB){
            $zxTime=$this->SummerTime2;
        }else{
            $zxTime=$this->WinterTime2;  
        }
        $w=date("w");
        for($i=0;$i<10;$i++){
            if($this->timeIn($zxTime[$i],$zxTime[$i+1])){
                if($i%2==0){//上课时间
                    $tempR=$this->getClassByWI($w,$kIndex[$i]+1);
                    if($tempR!=""){
                        $r=$tempR;
                    }
                    $tempR=$this->getNextClassByWI($w,$kIndex[$i]+1);
                    if($tempR!=""){
                        $r.="\n".$tempR;
                    }
					$r=trim($r);
                }else{//课间时间
                    $tempR=$this->getNextClassByWI($w,$kIndex[$i]+1);
                    if($tempR!=""){
                        $r.="\n".$tempR;
                    }
					$r=trim($r);
                }
            }
        }//for
        return $r;
    }

    //当天课程
    public function getTodayClass(){
        if($this->getWeekOf20()%2==1){
            $k=$this->A;
        }else{
            $k=$this->B;
        }
        $w=date("w");
        $r1="";//一天课程信息
        for($i=0;$i<5;$i++){
            if(trim($k[$w*5+$i])!=''){
                $k1=explode("|",trim($k[$w*5+$i]));
                $r1.="节次：".substr($k1[5], 6)."\n";
                $r1.="时间：".trim($k1[6])."\n";
                $r1.="老师：".trim($k1[1])."\n";
                $r1.="教室：".trim($k1[7])."\n";
                $r1.="课程：".trim($k1[0])."\n";
                $r1.= "同学：<a href=\"".API_URL."weixin/web/index.php?opid=_OPID&action=txlb&xkkh=".trim($k1[2])."\">点击此处查看</a>\n".$this->line1;
            }
        }
        if($r1!=""){
            $r="【星期".$this->weekName[$w]."】\n".substr($r1,0,-(strlen($this->line1)));  
        }else{
            $r="今天没有课哦，好好休息吧！！！";
        }
        return $r;
    }

    //明天课程
    public function getNextdayClass(){
        if($this->getWeekOf20()%2==1){
            $k=$this->A;
        }else{
            $k=$this->B;
        }
        $w=(date("w")+1)%7;//明天星期几
        $r1="";//一天课程信息
        for($i=0;$i<5;$i++){
            if(trim($k[$w*5+$i])!=''){
                $k1=explode("|",trim($k[$w*5+$i]));
                $r1.="节次：".substr($k1[5], 6)."\n";
                $r1.="时间：".trim($k1[6])."\n";
                $r1.="老师：".trim($k1[1])."\n";
                $r1.="教室：".trim($k1[7])."\n";
                $r1.="课程：".trim($k1[0])."\n";
                $r1.= "同学：<a href=\"".API_URL."weixin/web/index.php?opid=_OPID&action=txlb&xkkh=".trim($k1[2])."\">点击此处查看</a>\n".$this->line1;
            }
        }
        if($r1!=""){
            $r="【星期".$this->weekName[$w]."】\n".substr($r1,0,-(strlen($this->line1)));  
        }else{
            $r="【星期".$this->weekName[$w]."】\n今天没有课哦，好好休息吧！！！";
        }
        return $r;
    }

    //本周课程
    public function getWeekClass(){
        if($this->getWeekOf20()%2==1){
            $k=$this->A;
        }else{
            $k=$this->B;
        }
        for($w=0;$w<7;$w++){
            $r1="";//一天课程信息
            for($i=0;$i<5;$i++){
                if(trim($k[$w*5+$i])!=''){
                    $k1=explode("|",trim($k[$w*5+$i]));
                    $r1.="节次：".substr($k1[5], 6)."\n";
                    $r1.="课程：".$k1[0]."\n".$this->line1;
                }
            }
            if($r1!=""){
                $r.="【星期".$this->weekName[$w]."】\n".substr($r1,0,-(strlen($this->line1))).$this->line2;  
            }
        }
        $r="【第".$this->getWeekOf20()."周课程】\n".$this->line2.substr($r,0,-(strlen($this->line2)));
        return $r;
    }

    //根据星期第几节课获取课程信息
    //0是周日//1是第一节课
    public function getClassByWI($w,$i){

        if($this->getWeekOf20()%2==1){
            $k=$this->A;
        }else{
            $k=$this->B;
        }

        $i=$i-1;
        if(trim($k[$w*5+$i])!=''){
            $k1=explode("|",trim($k[$w*5+$i]));
            $r1.="节次：".substr($k1[5], 6)."\n";
            $r1.="时间：".trim($k1[6])."\n";
            $r1.="老师：".trim($k1[1])."\n";
            $r1.="教室：".trim($k1[7])."\n";
            $r1.="课程：".$k1[0]."\n";
            $r1="【当前课程】\n".$r1;
        }
        return trim($r1);
    }

    //根据星期第几节课获取下一次课程信息
    //0是周日//1是第一节课
    public function getNextClassByWI($w,$i){

        if($this->getWeekOf20()%2==1){
            $k=$this->A;
        }else{
            $k=$this->B;
        }
		
        
        $x=$w*5+$i;//下一节课的索引
        while(trim($k[$x])==''){
            if($x<34){
                $x++;
            }else{
                $x=0;
            }
        }

        if(trim($k[$x])!=''){
            $k1=explode("|",trim($k[$x]));
            $r1.="节次：周".$this->weekName[(($x-$x%5)/5)].'  '.substr($k1[5], 6)."\n";
            $r1.="时间：".trim($k1[6])."\n";
            $r1.="老师：".trim($k1[1])."\n";
            $r1.="教室：".trim($k1[7])."\n";
            $r1.="课程：".$k1[0]."\n";

            $r="【下一次课】\n".$r1; 
        }
        return trim($r);
    }
    //当前学期第几周
    private function getWeekOf20(){
        $a = intval(date('W',strtotime(date("Y").'-'.KXSJ)));
        $b = intval(date('W',time()+24*60*60));
        return ($b-$a+1);
    }

    private function timeIn($t1,$t2){
        $r=false;
        $t=time() ;
        //$t=strtotime("23:33:00");
        $t11=$t1;
        $t1=strtotime($t1.":00");
        
        if($t2=="08:00+1"){
            if(((int)date("H",$t))<8){
                $t1=strtotime($t11.":00")-1*24*60*60;
                $t2=strtotime("08:00:00");
            }else{
                $t2=strtotime("08:00:00")+1*24*60*60; 
            }
        }else{
            $t2=strtotime($t2.":00");
        }
        
        if(($t1-$t)<=0 && ($t2-$t)>=0){
            $r=true;
        }
        return $r;
    }

    public function getClassMateList($xkkh){
        $code=Tools::getHttp(DZU_URL."student.asp?xkkh=".$xkkh);
        $lib = explode("@XSB",$code);
        for($i=1;$i<count($lib);$i++){
            $XH = Tools::getInfo(trim($lib[$i]),"XH");
            $SFZH = Tools::getInfo(trim($lib[$i]),"SFZH");
            $XM = Tools::getInfo(trim($lib[$i]),"XM");
            $XB = Tools::getInfo(trim($lib[$i]),"XB");
            $XY = Tools::getInfo(trim($lib[$i]),"XY");
            $ZYMC = Tools::getInfo(trim($lib[$i]),"ZYMC");
            $DQSZJ = Tools::getInfo(trim($lib[$i]),"DQSZJ");

            if($XB=='男'){
                $image='boy.png';
            }else{
                $image='girl.png';
            }
            $str .= HTML::makeList($image,$XM."(".$DQSZJ."级)",$ZYMC,"");
        }//for
        if($str==''){
            $str=HTML::makeListNULL("没有同学");
        }
        $tpl=file_get_contents(API_URL."weixin/web/list.html");
        $tpl=str_replace("_FORM", '', $tpl);
        $tpl=str_replace("_TITLE", '我的同学('.(count($lib)-1).')', $tpl);
        $tpl=str_replace("_LIST", $str, $tpl);
        return $tpl;
    }
}
?>